package inter4.controller;/*
    @Auther:kongshan
    @Date:2023/6/6-06-06-21:39
    @Description:IntelliJ IDEA
    @version:


*/

import inter4.pojo.Education;
import inter4.pojo.Student;
import inter4.service.EducationService;
import inter4.service.StudentService;
import inter4.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class StudentController {
    @Autowired
    StudentService studentService;
    @Autowired
    EducationService educationService;

    @RequestMapping("/saveStudent")
    //新增数据
    public ModelAndView saveStudent(Student student){
        ModelAndView mv=new ModelAndView();
        int num=studentService.saveStudent(student);
        if (num>0){
            mv.setViewName("redirect:listStudent");
            return mv;//转到列表页
        }else{
            mv.setViewName("redirect:showSaveStudent");
            return mv;
        }
    }

    @RequestMapping("/listStudent")
    public ModelAndView listStudent(){
        ModelAndView mv=new ModelAndView();
        List<Student> studentList=studentService.showAllStudent();//获取业务列表
        List<Education> eduList=educationService.showAllEdu();//获取字典表
        mv.addObject("eduList",eduList);
        mv.addObject("studentList",studentList);
        mv.setViewName("/student/listStudent");
        return mv;
    }

    @RequestMapping(value = "updateStudent")
//    修改提交
    public String updateStudent(Student student){
        int num=studentService.updateStudent(student);
        return "forward:studentList";
    }

    //显示修改页面
    @RequestMapping(value = "/editUser")
    public ModelAndView editStudent(int bh){
        ModelAndView mv=new ModelAndView();
        Student student=studentService.showStudentByBh(bh);
        List<Education> eduList=educationService.showAllEdu();
        mv.addObject("eduList",eduList);
        mv.addObject("student",student);//绑定修改实体对象
        mv.setViewName("/student/editStudent");
        return mv;
    }
//    @RequestMapping(value = "/tipStudent")
//    //根据编号显示实体信息
//    public ModelAndView showStudentByBh(int bh){
//        ModelAndView mv=new ModelAndView();
//        Student student=studentService.showStudentByBh(bh);
//        mv.setViewName("tipStudent");
//        mv.addObject("student",student);
//        return mv;
//    }

//    //根据编号删除一条数据
//    @RequestMapping("/delStudent")
//    public String  delStudent(int bh){
//        int num=studentService.delStudent(bh);
//        return "redirect:studentList";
//    }

    @RequestMapping("/showEditStudent")
    //显示修改页面
    public ModelAndView showEditStudent(int bh){
        ModelAndView mv=new ModelAndView();
        Student student=studentService.getStudentByBh(bh);//获取修改实体
        List<Education> eduList=educationService.showAllEdu();
        mv.addObject("eduList",eduList);
        mv.addObject("student",student);//绑定修改实体对象
        mv.setViewName("/student/editStudent");
        return mv;
    }


    @RequestMapping(value = "/saveStudent")
    public ModelAndView showSaveStudent(){
        ModelAndView mv=new ModelAndView();
        List<Education> list=educationService.showAllEdu();
        mv.setViewName("/student/editStudent");
        mv.addObject("edulist",list);
        return mv;
    }
    public StudentService getStudentService() {
        return studentService;
    }

    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }
}
